﻿using CommunityToolkit.Mvvm.DependencyInjection;
using StickyNote.Core;
using StickyNote.ViewModels;
using StickyNote.Views;
using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace StickyNote.Services
{
    public class NoteService
    {
        private JotService fJotService = Ioc.Default.GetRequiredService<JotService>();

        private List<NoteWindow> fNoteWindows = new List<NoteWindow>();

        public List<NoteWindow> NoteWindows
        {
            get
            {
                return this.fNoteWindows;
            }
        }

        /// <summary>
        /// 初始化便签窗体列表
        /// </summary>
        /// <param name="noteNames"></param>
        public void InitNoteWindows(string[] noteNames)
        {
            for (int i = 0; i < noteNames.Length; i++)
            {
                NoteWindow window = new NoteWindow();
                window.Name = noteNames[i];
                window.WindowNo = int.Parse(this.fJotService.GetData(noteNames[i], "WindowNo", "0").ToString());

                NoteWindowViewModel vm = window.DataContext as NoteWindowViewModel;
                if (vm != null)
                {
                    vm.Note = this.fJotService.GetData(noteNames[i], "Note", string.Empty).ToString();
                }

                this.fNoteWindows.Add(window);

                string visibleStr = IniHelper.GetValue(E_Sections.便签显示属性.ToString(), window.Name);
                int visible = int.Parse(string.IsNullOrEmpty(visibleStr) ? "0" : visibleStr);
                if (visible == 1)
                {
                    window.Show();
                }
            }
        }

        /// <summary>
        /// 新建便利贴
        /// </summary>
        public void AddNoteWindow()
        {
            NoteWindow window = new NoteWindow();
            this.fNoteWindows.Add(window);
            window.Left = SystemParameters.WorkArea.Size.Width - window.Width - 50;
            window.Top = 50;
            window.Name = $"NoteWindow{this.fNoteWindows.Count}";
            window.Title = $"便利贴{this.fNoteWindows.Count}";
            window.WindowNo = this.fNoteWindows.Count;
            window.Show();
            IniHelper.SetValue(E_Sections.便签显示属性.ToString(), window.Name, "1");
        }

        /// <summary>
        /// 移除便利贴
        /// </summary>
        /// <param name="name"></param>
        public void RemoveNoteWindow(string name)
        {
            NoteWindow? window = this.fNoteWindows.FirstOrDefault(p => p.Name == name);
            if (window != null)
            {
                window.Close();
                this.fNoteWindows.Remove(window);
            }
        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="name"></param>
        public void CloseNoteWindow(string name)
        {
            NoteWindow? window = this.fNoteWindows.FirstOrDefault(p => p.Name == name);
            if (window != null)
            {
                window.Close();
                IniHelper.SetValue(E_Sections.便签显示属性.ToString(), name, "0");
            }
        }
    }
}
